package com.twt.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Maps;
import com.twt.common.utils.Assert;
import com.twt.model.system.entity.SysDictItem;
import com.twt.sys.base.AbstractBaseService;
import com.twt.sys.mapper.SysDictMapper;
import com.twt.sys.service.SysDictItemService;
import com.twt.sys.service.SysDictService;
import com.twt.model.system.entity.SysDict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysDictServiceImpl extends AbstractBaseService<SysDictMapper, SysDict> implements SysDictService {
    @Autowired
    private SysDictItemService sysDictItemService;

    @Override
    public void deleteByIds(String ids) {
        Assert.isBlank(ids, "删除主键不能为空");
        for (String dictId : Arrays.asList(ids.split(","))) {
            List<SysDictItem> sysDictItemList = this.sysDictItemService.queryByDictId(dictId);
            Assert.isNotEmpty(sysDictItemList, "存在字典项未删除，请先删除字典项");
            this.removeById(dictId);
        }
    }

    @Override
    protected void beforeSave(SysDict po) {
        Assert.isBlank(po.getDictCode(), "字典代码 不能为空！");
        Assert.isBlank(po.getDictName(), "字典名称 不能为空！");
    }

    @Override
    public void editById(SysDict editVo) {
        Assert.isBlank(editVo.getId(),"主键不能为空");
        this.beforeSave(editVo);


    }

    @Override
    public Page<SysDict> queryPageByQueryVo(Page page, SysDict sysDict) {
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("dict_id",dictId);

        return this.page(page,queryWrapper);
    }

    @Override
    public Map<String,Object> queryAllDict() {
        Map<String,Object> dictMap = Maps.newHashMap();

        List<SysDict> list = this.list();
        for (SysDict sysDict:list){
            List<SysDictItem> sysDictItems = this.sysDictItemService.queryByDictId(sysDict.getId());
            dictMap.put(sysDict.getDictCode(),sysDictItems);
        }
        return dictMap;
    }
}
